package se.webbzon.boltzmann.editor.factory.propertyframe;

import java.awt.GridLayout;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import se.webbzon.boltzmann.editor.component.dependencylist.DependencyComboBox;
import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.boltzmann.editor.propertyframe.WorldObjectPropertyFrame;
import se.webbzon.boltzmann.editor.propertyframe.WorldStaticObjectPropertyFrame;
import se.webbzon.boltzmann.game.object.waypoint.PathProgramEnum;
import se.webbzon.boltzmann.game.object.waypoint.Target;
import se.webbzon.boltzmann.game.object.waypoint.Waypoint;
import se.webbzon.oschi01.worldobject.WorldObject;

public class TargetPropertyFrameFactory implements PropertyFrameFactory {
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public String getWorldObjectName() {
		return Target.worldObjectName;
	}

	@Override public WorldObjectPropertyFrame makePropertyFrame(EditorModel model,
																WorldObject instance) {
		return new PropertyFrameImpl(model,(Target) instance);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	/** An implementation of a property frame. **/
	private static final class PropertyFrameImpl extends WorldStaticObjectPropertyFrame {
		
		// The combo boxes of this panel
		private final DependencyComboBox<Waypoint> waypointInput;
		private final JComboBox<PathProgramEnum> programInput;
		
		// The text fields of this panel
		private final JTextField timeInput, durationInput;
	
		/*============================================================
		Constructors
		============================================================*/
	
		public PropertyFrameImpl(EditorModel model, Target t) {
			super(model,t);
			
			// Create target panel
			JPanel targetPanel = new JPanel();
			targetPanel.setLayout(new BoxLayout(targetPanel,BoxLayout.Y_AXIS));
			waypointInput = 
					new DependencyComboBox<Waypoint>(	model,Waypoint.class,
														t.getWaypoint(),
														"Start Waypoint: ");
			waypointInput.setBorder(BorderFactory.createTitledBorder(waypointInput.getName()));
			targetPanel.add(waypointInput);
			
			JPanel programPanel = new JPanel();
			programPanel.setBorder(BorderFactory.createTitledBorder("Target Program:"));
			programPanel.setLayout(new GridLayout(3,2));
			programPanel.add(new JLabel("Program:"));
			programInput = new JComboBox<PathProgramEnum>(PathProgramEnum.values());
			programInput.setSelectedItem(t.getProgram());
			programPanel.add(programInput);
			programPanel.add(new JLabel("Cycle Time (# of steps):"));
			timeInput = new JTextField("" + t.pos());
			programPanel.add(timeInput);
			programPanel.add(new JLabel("Cycle Duration (# of steps):"));
			durationInput = new JTextField("" + t.getCycleDuration());
			programPanel.add(durationInput);
			targetPanel.add(programPanel);
			addPanel("Target",targetPanel);
			
			initialize();
		}
	
		/*============================================================
		Public Methods
		============================================================*/
	
		public boolean apply(Collection<String> errorMessages) {
			if (super.apply(errorMessages)) try {
				Target t = (Target) getInstance();
				t.setWaypoint(waypointInput.getInstance());
				t.setProgram((PathProgramEnum) programInput.getSelectedItem());
				t.setCycleDuration(Integer.parseInt(durationInput.getText()));
				t.seek(Integer.parseInt(timeInput.getText()));
				return true;
			} catch (NumberFormatException e) {
				errorMessages.add(e.getMessage());
				return false;
			} else
				return false;
		}
	
	}
	
}



